<?php
/**
 * Created by glq.
 * Date: 2018/10/21
 * Time: 10:48
 */

namespace Label\Controller;

use Common\Controller\AdminbaseController;
use Label\Model\CategoryModel;

class CategoryAdminController extends AdminbaseController{

    protected $category_model;
    protected $page_size;

    function _initialize() {
        parent::_initialize();
        $this->page_size            = 20;
        $this->category_model       = D("Label/Category");
    }

    // 类型列表
    public function category_list() {
        $where = array();
        if ($type = I('type')) {
            $where['type'] = $type;
            $this->assign('type', $type);
        }
        $category_model = new CategoryModel();
        $this->assign('type_list', $category_model->category_types);
        $categorys = $this->category_model->where($where)->order('type asc, sort desc, id asc')->select();

        $this->assign('categorys', $categorys);

        $this->display();
    }

    // 新增/编辑分类
    public function category_edit()
    {
        if (IS_POST) {
            $id     = I("post.id", 0, 'intval');
            $data   = array(
                'type'      => I('type'),
                'name'      => I('name'),
                'status'    => I('status'),
            );

            if (!pre_data($data)) {
                $this->error('请完善必要信息');
            }

            if ($id) {
                $this->category_model->where(['id' => $id])->save($data);
                $this->success('修改成功');

            } else {
                $re = $this->category_model->add($data);

                if ($re) {
                    $this->success('创建成功');
                } else {
                    $this->error('创建失败');
                }
            }
        } else {
            $id     = I("get.id", 0, 'intval');
            $info   = $this->category_model->where(['id' => $id])->find();

            $category_model = new CategoryModel();
            $this->assign('type_list', $category_model->category_types);

            if ($id) {
                if ($info) {

                    if (I("get.delete", 0, 'intval')) {
                        $re = $this->category_model->where(['id' => $id])->delete();

                        if ($re) {
                            $this->success('删除成功');
                        } else {
                            $this->error('删除失败');
                        }

                    } else {
                        $this->assign('info', $info);
                        $this->display();
                    }

                } else {
                    $this->error('数据错误或已删除');
                }
            } else {
                $this->display();
            }
        }
    }

    public function change_category_sort()
    {
        $id     = I('id', 0, 'intval');
        $order  = I('order', 0, 'intval');

        if ($id) {
            $this->category_model->where(['id' => $id])->save(['sort' => $order]);
            $this->success('修改成功');
        }
    }
}